package com.gzhryc.net_marketing.cloud.business.services;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.NumberTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.business.PayConfig;
import com.gzhryc.net_marketing.cloud.business.dao.TeamPayApplyMemberDao;
import com.gzhryc.net_marketing.cloud.business.dao.db.PayGoodsOrder;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApply;
import com.gzhryc.net_marketing.cloud.business.dao.db.TeamPayApplyMember;
import com.gzhryc.net_marketing.cloud.business.dao.enums.TeamPayApplyMemberEnum;
import com.gzhryc.net_marketing.cloud.business.dao.models.TeamPayApplyMemberStateCount;
import com.gzhryc.net_marketing.cloud.business.services.dto.TeamPayApplyMemberSearch;
import com.gzhryc.net_marketing.cloud.goods.dao.db.Goods;
import com.gzhryc.net_marketing.cloud.goods.services.GoodsService;
import com.gzhryc.pay.adaptation.weixin.WXPayNormalPayService;
import com.gzhryc.pay.adaptation.weixin.enums.EWXPayNormalRefundState;
import com.gzhryc.pay.adaptation.weixin.msg.normal.WXPayNormalRefundResponse;
import com.gzhryc.pay.models.IPayOrder;
import com.gzhryc.shared_device.base.enums.EPayState;
import com.gzhryc.shared_device.base.enums.EPayType;
import com.gzhryc.shared_device.base.enums.ERefundState;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WXRefundOrder;
import com.gzhryc.shared_device.common.payment.dao.db.WalletPayOrder;
import com.gzhryc.shared_device.common.payment.services.WXPayOrderService;
import com.gzhryc.shared_device.common.payment.services.WXRefundOrderService;
import com.gzhryc.shared_device.oem.code.member.services.MemberWalletService;

public class TeamPayApplyMemberService {

	static Logger log = Logger.getLogger(TeamPayApplyMemberService.class);

	public static TeamPayApplyMemberService self() {
		return MultiDBTools.getService(TeamPayApplyMemberService.class, true);
	}

	public static TeamPayApplyMemberService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey, TeamPayApplyMemberService.class, true);
	}

	TeamPayApplyMemberDao dao;

	public TeamPayApplyMemberService(String jdbcKey) {
		dao = new TeamPayApplyMemberDao(jdbcKey);
	}

	public List<TeamPayApplyMember> findBySearch(TeamPayApplyMemberSearch search, Integer pageNo, Integer pageLimit){
		Conditions condition = dao.buildConditions(search);
		condition.and().in("state",search.getStateList());

		try {
			return dao.findByCondition(condition,pageNo,pageLimit);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
		}
		return null;
	}

	public List<TeamPayApplyMember> findByApplyId(Long applyId) {
		if (applyId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyId);
			try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public List<TeamPayApplyMember> findByStateAndApplyIds(Integer state, List<Long> applyIds){
		if (state != null && applyIds != null && applyIds.size() > 0) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("state",state);
			condition.and().in("applyId", applyIds);
			try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	/**
	 * 获取参与中的会员
	 * @param applyId
	 * @return
	 */
	public List<TeamPayApplyMember> findAttendByApplyId(Long applyId) {
		if (applyId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyId);
			condition.and().not("state", TeamPayApplyMemberEnum.State.Waiting.index());
			try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public List<TeamPayApplyMember> findByApplyIdAndState(Long applyId, Integer state) {
		if (applyId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyId);
			condition.and().is("state", state);
			try {
				return dao.findByCondition(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public List<TeamPayApplyMemberStateCount> findStateCountByMemberId(Long memberId){
		if(memberId != null){
			Conditions conditions = new Conditions(TeamPayApplyMember.class);
			conditions.and().is("memberId",memberId);
			try {
				return dao.countByGroupState(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return null;
	}

	public Long countByMemberId(Long memberId){
		if(memberId != null){
			Conditions conditions = new Conditions(TeamPayApplyMember.class);
			conditions.and().is("memberId",memberId);
			try {
				return dao.count(conditions);
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return 0L;
	}

	public TeamPayApplyMember getByApplyIdAndMemberId(Long applyId, Long memberId) {
		if (applyId != null && memberId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyId);
			condition.and().is("memberId", memberId);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}

		return null;
	}

	public TeamPayApplyMember getParticipatingByGoodsIdAndMemberId(Long goodsId, Long memberId) {
		if (goodsId != null && memberId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("goodsId", goodsId);
			condition.and().is("memberId", memberId);
			condition.and().is("state", TeamPayApplyMemberEnum.State.Participating.index());
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public TeamPayApplyMember getWaitingByGoodsIdAndMemberId(Long goodsId, Long memberId) {
		if (goodsId != null && memberId != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("goodsId", goodsId);
			condition.and().is("memberId", memberId);
			condition.and().is("state", TeamPayApplyMemberEnum.State.Waiting.index());
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public TeamPayApplyMember getByPayTradeNo(String payTradeNo) {
		if (StringUtils.isNotBlank(payTradeNo)) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("payTradeNo", payTradeNo);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public TeamPayApplyMember getByBusinessTradeNo(String businessTradeNo) {
		if (StringUtils.isNotBlank(businessTradeNo)) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("businessTradeNo", businessTradeNo);
			try {
				return dao.get(condition);
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return null;
	}

	public boolean closeApply(TeamPayApplyMember simpleTeamPayApplyMember, String closeReason) throws LogicException {
		if(simpleTeamPayApplyMember != null && StringTools.isNotBlank(closeReason)){
			if(!TeamPayApplyMemberEnum.State.Waiting.index().equals(simpleTeamPayApplyMember.getState())){
				throw new LogicException(simpleTeamPayApplyMember.getStateName() + "的拼团无法关闭");
			}

			TeamPayApply simpleTeamPayApply =  TeamPayApplyService.self().getById(simpleTeamPayApplyMember.getApplyId());
			if(simpleTeamPayApply == null){
				throw new LogicException("未找到拼团信息");
			}

			Conditions conditions = new Conditions(TeamPayApplyMember.class);
			conditions.and().is("applyId",simpleTeamPayApplyMember.getApplyId());
			conditions.and().is("memberId",simpleTeamPayApplyMember.getMemberId());
			conditions.and().is("state", TeamPayApplyMemberEnum.State.Waiting.index());

			TeamPayApplyMember updateEntity = new TeamPayApplyMember();
			updateEntity.setState(TeamPayApplyMemberEnum.State.Close.index());
			updateEntity.setCloseReason(closeReason);

			JdbcSession.begin();
			try {
				if(dao.updateNotNull(updateEntity,conditions) > 0){
					if(simpleTeamPayApply.getMemberId().equals(simpleTeamPayApplyMember.getMemberId())){
						//发起人关闭，则拼团活动也关闭
						if(TeamPayApplyService.self().closeApply(simpleTeamPayApply)){
							JdbcSession.commit();
							return true;
						}
					}else{
						JdbcSession.commit();
						return true;
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			} finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public TeamPayApplyMember payResult(WXPayOrder payOrder,OperateInfo operateInfo) {
		if (payOrder != null) {
			TeamPayApplyMember applyMember = this.getByPayTradeNo(payOrder.getTradeNo());
			if (applyMember != null) {
				if (TeamPayApplyMemberEnum.State.Participating.index().equals(applyMember.getState())) {
					log.warn("会员已支付参与，会员信息：{{0:json}}", applyMember);
					return null;
				}

				Conditions condition = new Conditions(TeamPayApplyMember.class);
				condition.and().is("applyId", applyMember.getApplyId());
				condition.and().is("memberId", applyMember.getMemberId());

				TeamPayApplyMember updateEntity = new TeamPayApplyMember();
				if (EPayState.Success.index().equals(payOrder.getPayStatus())) {
					updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.Success.index());
					updateEntity.setState(TeamPayApplyMemberEnum.State.Participating.index());
				} else if (EPayState.Fail.index().equals(payOrder.getPayStatus())) {
					updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.Fail.index());
				}

				try {
					if (dao.updateNotNull(updateEntity, condition) > 0) {
						applyMember.setPayState(updateEntity.getPayState());
						if (updateEntity.getState() != null) {
							applyMember.setState(updateEntity.getState());
						}
						if (TeamPayApplyMemberEnum.PayState.Success.index().equals(updateEntity.getPayState())) {
							operateInfo.setOperateNote("参加团拼" + applyMember.getApplyId());
							Long money = NumberTools.changeMoneyLi(NumberTools.changeMoney(payOrder.getReceiptMoney()));
							if (!MemberWalletService.self().addFreezeMoney(applyMember.getMemberId(), money, operateInfo)) {
								log.error("团拼修改钱包失败，参加会员信息：{{0:json}}", applyMember);
							}
						}
						return applyMember;
					}
				} catch (SQLException e) {
					log.error(e.getMessage(), e);
				}
			} else {
				log.error("未获取团拼会员信息，订单：{{0}}", payOrder.getTradeNo());
			}
		}
		return null;
	}
	
	public TeamPayApplyMember payResult(WalletPayOrder payOrder,OperateInfo operateInfo) {
		if (payOrder != null) {
			TeamPayApplyMember applyMember = this.getByPayTradeNo(payOrder.getTradeNo());
			if (applyMember != null) {
				if (TeamPayApplyMemberEnum.State.Participating.index().equals(applyMember.getState())) {
					log.warn("会员已支付参与，会员信息：{{0:json}}", applyMember);
					return null;
				}

				Conditions condition = new Conditions(TeamPayApplyMember.class);
				condition.and().is("applyId", applyMember.getApplyId());
				condition.and().is("memberId", applyMember.getMemberId());

				TeamPayApplyMember updateEntity = new TeamPayApplyMember();
				if (EPayState.Success.index().equals(payOrder.getPayStatus())) {
					updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.Success.index());
					updateEntity.setState(TeamPayApplyMemberEnum.State.Participating.index());
				} else if (EPayState.Fail.index().equals(payOrder.getPayStatus())) {
					updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.Fail.index());
				}

				try {
					if (dao.updateNotNull(updateEntity, condition) > 0) {
						applyMember.setPayState(updateEntity.getPayState());
						if (updateEntity.getState() != null) {
							applyMember.setState(updateEntity.getState());
						}
						if (TeamPayApplyMemberEnum.PayState.Success.index().equals(updateEntity.getPayState())) {
							operateInfo.setOperateNote("参加团拼" + applyMember.getApplyId());
							Long money = NumberTools.changeMoneyLi(NumberTools.changeMoney(payOrder.getPayMoney()));
							if (!MemberWalletService.self().addFreezeMoney(applyMember.getMemberId(), money, operateInfo)) {
								log.error("团拼修改钱包失败，参加会员信息：{{0:json}}", applyMember);
							}
						}
						return applyMember;
					}
				} catch (SQLException e) {
					log.error(e.getMessage(), e);
				}
			} else {
				log.error("未获取团拼会员信息，订单：{{0}}", payOrder.getTradeNo());
			}
		}
		return null;
	}


	public TeamPayApplyMember refundResult(TeamPayApplyMember applyMember, WXRefundOrder refundOrder){
		if(applyMember != null && refundOrder != null) {
			Conditions conditions = new Conditions(TeamPayApplyMember.class);
			conditions.and().is("applyId", applyMember.getApplyId());
			conditions.and().is("memberId", applyMember.getMemberId());

			TeamPayApplyMember updateEntity = new TeamPayApplyMember();
			if (ERefundState.Success.index().equals(refundOrder.getRefundStatus())) {
				updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.RefundSuccess.index());
			} else if (ERefundState.Fail.index().equals(refundOrder.getRefundStatus())) {
				updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.RefundError.index());
			}
			try {
				if(dao.updateNotNull(updateEntity, conditions) > 0){
					applyMember.setPayState(updateEntity.getPayState());
					return applyMember;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return applyMember;
	}

	public boolean add(TeamPayApplyMember applyMember) throws LogicException {
		if (applyMember.getApplyId() != null && applyMember.getMemberId() != null) {
			TeamPayApplyMember temp = getByApplyIdAndMemberId(applyMember.getApplyId(), applyMember.getMemberId());
			if (temp != null) {
				throw new LogicException("已参加该团拼");
			}
			applyMember.setCreateDate(new Date());
			try {
				return dao.insert(applyMember) > 0;
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			}
		}
		return false;
	}

	/**
	 * 再次支付
	 * @param applyMember
	 * @param payConfig
	 * @return
	 * @throws LogicException
	 */
	public IPayOrder againPay(TeamPayApplyMember applyMember, PayConfig payConfig) throws LogicException {
		Goods goods = GoodsService.self().getById(applyMember.getGoodsId());
		if (goods == null) {
			throw new LogicException("商品不存在");
		}
		
		/*
		PayOrder payOrder = new PayOrder();
		payOrder.setTitle(goods.getName());
		payOrder.setBusinessTradeNo(applyMember.getPayTradeNo());
		payOrder.setPayType(payConfig.getPayType());
		payOrder.setTradeType(TradeType.Normal.name());
		payOrder.setAppId(payConfig.getAppId());
		payOrder.setPayerId(payConfig.getPayerId());
		payOrder.setPayMoney(goods.getTeamPayMoney());
		payOrder.setBusinessAccount(payConfig.getBusinessAccount());
		payOrder.setClientIP(payConfig.getClientIP());
		payOrder.setNotifyUrl(payConfig.getNotifyUrl());

		JdbcSession.begin();
		try {
			if (PayOrderService.self().add(payOrder)) {
				Conditions condition = new Conditions(TeamPayApplyMember.class);
				condition.and().is("applyId",applyMember.getApplyId());
				condition.and().is("memberId",applyMember.getMemberId());

				TeamPayApplyMember updateEntity = new TeamPayApplyMember();
				updateEntity.setPayTradeNo(payOrder.getTradeNo());
				updateEntity.setPayType(payOrder.getPayType());
				updateEntity.setPayMoney(payOrder.getPayMoney());
				updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.Waiting.index());
				updateEntity.setState(TeamPayApplyMemberEnum.State.Waiting.index());

				if(dao.updateNotNull(updateEntity,condition) > 0){
					ClassTools.copyNotNull(applyMember,updateEntity);
					JdbcSession.commit();
					return payOrder;
				}
			}
		}catch (SQLException e) {
			log.error(e.getMessage(),e);
		}finally{
			JdbcSession.end();
		}*/
		return null;
	}

	public boolean toFinish(TeamPayApply payApply, TeamPayApplyMember applyMember,OperateInfo operateInfo) {
		if (applyMember != null) {
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyMember.getApplyId());
			condition.and().is("memberId", applyMember.getMemberId());
			condition.and().is("state", TeamPayApplyMemberEnum.State.Participating.index());

			TeamPayApplyMember updateEntity = new TeamPayApplyMember();
			updateEntity.setState(TeamPayApplyMemberEnum.State.Finish.index());
			updateEntity.setRewardMoney(applyMember.getRewardMoney());

			PayGoodsOrder simpleTeamPayOrder = null;
			JdbcSession.begin();
			try {
				if (dao.updateNotNull(updateEntity, condition) > 0) {
					Long payMoney = NumberTools.changeMoneyLi(NumberTools.changeMoney(applyMember.getPayMoney()));
					operateInfo.setOperateNote("团拼" + applyMember.getApplyId() + "成功");
					if (MemberWalletService.self().reduceFreezeMoney(applyMember.getMemberId(),payMoney,operateInfo)) {
						if(applyMember.getRewardMoney() > 0) {
							//增加奖励金额
							Long rewardMoney = NumberTools.changeMoneyLi(NumberTools.changeMoney(applyMember.getRewardMoney()));
							operateInfo.setOperateNote("团拼" + applyMember.getApplyId() + "成功，发放奖励");
							if(!MemberWalletService.self().addVMoney(applyMember.getMemberId(),rewardMoney,operateInfo)){
								//奖励金额添加失败不影响订单创建
								log.error("团拼成功，发放奖励失败，团拼会员：{{0:json}}",applyMember);
							}
						}
						//创建订单
						simpleTeamPayOrder = PayGoodsOrderService.self().payTeamApplyMember(payApply,applyMember);
						if(simpleTeamPayOrder != null) {
							JdbcSession.commit();
							return true;
						}
					}
				}
			} catch (SQLException e) {
				log.error(e.getMessage(), e);
			} catch (LogicException e) {
				log.error(e.getMessage(), e);
			} finally {
				JdbcSession.end();
			}
		}
		return false;
	}

	public boolean toDrain(TeamPayApplyMember applyMember,WXPayNormalPayService payService,OperateInfo operateInfo) {
		if (applyMember != null) {
			boolean flag = false;

			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyMember.getApplyId());
			condition.and().is("memberId", applyMember.getMemberId());
			condition.and().is("state", TeamPayApplyMemberEnum.State.Participating.index());

			TeamPayApplyMember updateEntity = new TeamPayApplyMember();
			updateEntity.setState(TeamPayApplyMemberEnum.State.Drain.index());
			try {
				if (dao.updateNotNull(updateEntity, condition) > 0) {
					flag = true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}

			if(flag){
				condition = new Conditions(TeamPayApplyMember.class);
				condition.and().is("applyId", applyMember.getApplyId());
				condition.and().is("memberId", applyMember.getMemberId());
				condition.and().is("payState", TeamPayApplyMemberEnum.PayState.Success.index());

				updateEntity = new TeamPayApplyMember();
				updateEntity.setPayState(TeamPayApplyMemberEnum.PayState.RefundSuccess.index());

				JdbcSession.begin();
				try {
					if (dao.updateNotNull(updateEntity, condition) > 0) {
						if (EPayType.EWallet.index().equals(applyMember.getPayType())) {
							//TODO 临时处理
							Long payMoney = NumberTools.changeMoneyLi(NumberTools.changeMoney(applyMember.getPayMoney()));
							if (MemberWalletService.self().unfreezeMoney(applyMember.getMemberId(),payMoney,operateInfo)) {
								JdbcSession.commit();
								return true;
							}
						} else if (EPayType.WXPay.index().equals(applyMember.getPayType())) {
							WXPayOrder payOrder = WXPayOrderService.self(applyMember.getCreateDate()).getByTradeNo(applyMember.getPayTradeNo());
							if (payOrder != null) {
								if (payService != null) {
									WXRefundOrder refundOrder = WXRefundOrderService.self(applyMember.getCreateDate()).createRefundOrder(payOrder
											, payOrder.getReceiptMoney(),operateInfo.getOperateNote());
									WXPayNormalRefundResponse externalResult = payService.applyRefundOrder(refundOrder);
									if (externalResult != null) {
										if (!EWXPayNormalRefundState.SUCCESS.name().equals(externalResult.getStatus())) {
											//RefundOrderService.self().toFail(refundOrder.getTradeNo(), JsonTools.toJson(externalResult));
										}else{
											//TODO 临时处理
											Long payMoney = NumberTools.changeMoneyLi(NumberTools.changeMoney(applyMember.getPayMoney()));
											MemberWalletService.self().unfreezeMoney(applyMember.getMemberId(),payMoney,operateInfo);
											JdbcSession.commit();
											return true;
										}
									} else {
										log.error("撤销订单请求未响应，支付订单信息：{{0:json}}", payOrder);
									}
								} else {
									log.error("撤销订单失败，未获取支付服务，支付订单信息：{{0:json}}", payOrder);
								}
							} else {
								log.error("撤销订单失败，未获取支付订单，订单号：{{0}}", applyMember.getPayTradeNo());
							}
						}
					}
				} catch (SQLException e) {
					log.error(e.getMessage(), e);
				} catch (LogicException e) {
					log.error(e.getMessage(), e);
				} finally {
					JdbcSession.end();
				}
			}else {
				TeamPayApplyMember temp = TeamPayApplyMemberService.self().getByApplyIdAndMemberId(applyMember.getApplyId(),applyMember.getMemberId());
				if(TeamPayApplyMemberEnum.State.Drain.index().equals(temp.getState())
						&& TeamPayApplyMemberEnum.PayState.RefundSuccess.index().equals(temp.getPayState())){
					return true;
				}
			}
		}
		return false;
	}

	public boolean toClose(TeamPayApplyMember applyMember) {
		if (applyMember != null) {
			if(TeamPayApplyMemberEnum.State.Close.index().equals(applyMember.getState())){
				return true;
			}

			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId", applyMember.getApplyId());
			condition.and().is("memberId", applyMember.getMemberId());
			condition.and().is("state", TeamPayApplyMemberEnum.State.Waiting.index());

			TeamPayApplyMember updateEntity = new TeamPayApplyMember();
			updateEntity.setState(TeamPayApplyMemberEnum.State.Close.index());
			try {
				if (dao.updateNotNull(updateEntity, condition) > 0) {
					return true;
				}
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}

	public boolean deleteByApplyId(Long applyId){
		if(applyId != null){
			Conditions condition = new Conditions(TeamPayApplyMember.class);
			condition.and().is("applyId",applyId);

			try {
				return dao.deleteByCondition(condition) >= 0;
			} catch (SQLException e) {
				log.error(e.getMessage(),e);
			}
		}
		return false;
	}
}
